import java.util.ArrayList;
import java.util.List;

/**
 * 回溯算法-电话号码的字母组合-力扣17
 */
public class Practice_15 {
        public static List<String> result = new ArrayList<>();
        public static StringBuffer path = new StringBuffer();
        public static String[] ini = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        public static void main(String[] args) {
            List<String> str = letterCombinations("2");
            System.out.println(str);
        }


        public static List<String> letterCombinations(String digits) {
            if (digits == null || digits.length() == 0) {
                return result;
            } else {
                int index = 0;
                backtracking(digits, index);
                return result;
            }
        }

        public static void backtracking(String digits, int index) {
            if (index == digits.length()) {
                result.add(path.toString());
                return;
            }
            int digit = Integer.valueOf(String.valueOf(digits.charAt(index)));
            String letter = ini[digit];
            for (int i = 0; i < letter.length(); i++) {
                path.append(String.valueOf(letter.charAt(i)));
                backtracking(digits, index + 1);
                path.deleteCharAt(path.length() - 1);
            }

        }
}
